package codetop.greedy;

import java.util.Arrays;
import java.util.Comparator;

public class Code435 {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1]-o2[1];
            }
        });
        int right=intervals[0][1];
        int ans=1;
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0]>=right){
                ans++;
                right=intervals[i][0];
            }
        }
        return intervals.length-ans;
    }
}
